<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
use Illuminate\Http\Request;
use App\Models\User;
class index extends Controller
{
    //前台首页
    function index(Request $request)
    {
          $users = DB::table('goods')
            ->orderBy('created_at', 'desc')
            ->get();

          $goods = DB::table('goods')
              ->orderBy('join', 'asc')
            ->get();

          $row = DB::table('goods')
            ->orderBy('join', 'desc')
            ->get();


        return view('index', ['goods' => $goods,'users' => $users,'row'=>$row]);

    }



    //前台个人信息页
    function my_sz_zh()
    {

        $value = session('login_name');
        $user = DB::table('user')->get();
        foreach ($user as $v)
        $username   = $v->username;//名字
        $userphone  = $v->userphone;//电话号码
        $gender     = $v->gender;//性别
        $userphoto  = $v->userphoto;//用户头像
        $usermoney  = $v->usermoney;//用户余额
        $createtime = $v->createtime;//创建时间
        return view('my_sz_zh',
                            ['userphoto' => $userphoto],
                            ['username' => $username],
                            ['userphone' => $userphone],
                            ['gender' => $gender],
                            ['usermoney' => $usermoney],
                            ['createtime' => $createtime]
                   );
    }



//全部商品
    function qbsp()
    {

        $goods = DB::table('goods')->get();

        return view('qbsp', ['goods' => $goods]);
    }
//最新开奖
    function zxkj()
    {
        $rows = DB::table('goods')->get();
        return view('zxkj',['rows' => $rows]);
    }
//常见问题
    function cjwt()
    {
        return view('cjwt');
    }
//夺宝记录
    function dbjl()
    {
        $rows = DB::table('goods')->get();

        return view('dbjl',['rows'=>$rows]);
    }
//商品详情页面
    function xq($id)
    {
        $xq_row = DB::table('goods')->where('id',$id)->get();

        foreach ($xq_row as $v){}
        $zrs = $v->need;
        $s_number = $v->join;
        $number = $zrs-$s_number;
        $img = $v->img;

        $bfba = round(($number/$zrs)*100,1);

        return view('xq',["xq_row" => $xq_row,'number'=>$number,'img'=>$img,'bfba'=>$bfba]);

    }
//往期揭晓
    function wqjx()
    {
        return view('wqjx');
    }
//所有参与记录
    function sycyjl()
    {
        return view('sycyjl');
    }
    //立即夺宝功能/用户获取幸运码
    function ljdb()
    {
        $stage=1;//第几期
        $data=date("dis");//当前时间分秒
        list($sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f',floatval($sec) * 100);//获取毫秒
        $lucky=$stage.$data.$msectime;//用户拿到的幸运码
        $name=session('login_name');
        $res=DB::table('user')->where(['username'=>$name])->get('usermoney');
        foreach($res as $v);//$v等于用户余额
        $a=$v->usermoney;
        if($a<1){return ['code'=>'-1','name'=>'先充值吧'];}
        $row=DB::table('user')->where(['username'=>$name])->update(['userlucky'=>$lucky,'usermoney'=>$a-1]);
        return ['code'=>'1','name'=>'夺宝成功'];
        if(session('login_name')==null)return redirect('denglu');//后期改成中间件
    }
//    夺宝规则
    function dbgz()
    {
        return view("dbgz");
    }
//抽取幸运码
    function draw(){
        $Total = array(
            1=>'121500665',
            2=>'121500666',
            3=>'121500621',
            4=>'121500668',
            5=>'121500669',
        );
        $total=$Total['1']+$Total['2']+$Total['3']+$Total['4']+$Total['5'];
        echo $total%5+10000011;
    }

}
